﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Script.Serialization;
using RBS.Core.Extension;
using RBS.Core.Model;
using RBS.Web.API.Models;

namespace RBS.Web.API.Areas.User.Controllers
{
    /// <summary>
    /// 食客信息
    /// </summary>
    public class ProfileController : ApiController
    {
        private ApiContext db = new ApiContext(); 
        /// <summary>
        /// 获取食客信息
        /// </summary>
        /// <param name="id">食客主键ID</param>
        /// <returns></returns>
        public JsonData GetU_User(int id)
        {
            var jsonData=new JsonData();
            U_User uUser = db.U_User.Find(id);
            if (uUser == null)
            {
                jsonData.Error = new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound)).Message;
                jsonData.Data = "null";
                jsonData.Success = false;

            }
            jsonData.Data = uUser ; ;
            jsonData.Success = false;
            return jsonData;
        }

        /// <summary>
        /// 获取食客信息
        /// </summary>
        /// <param name="uid">食客UID</param>
        /// <returns></returns>
        public U_User GetU_User(string uid)
        {
            var uUser = db.U_User.FirstOrDefault(u => u.U_Uid == uid || u.Email == uid);
            if (uUser == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }
            return uUser;
        }  
        /// <summary>
        /// 修改食客信息
        /// </summary>
        /// <param name="id">食客主键ID</param>
        /// <param name="uUser">食客实体</param>
        /// <returns></returns>
        public JsonData PutU_User([FromUri] int id, [FromBody]U_User uUser)
        {
            var jsonData=new JsonData();
            if (!ModelState.IsValid)
            {
                jsonData.Success = false;
                jsonData.Error=Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState).RequestMessage.ToString();
                return jsonData;
            }

            if (id != uUser.Id)
            {
                jsonData.Success = false;
                jsonData.Error = Request.CreateResponse(HttpStatusCode.BadRequest).RequestMessage.ToString();
                return jsonData;
            }  
            db.Entry(uUser).State = EntityState.Modified; 
            try
            {
                db.SaveChanges();
               jsonData.Data= Request.CreateResponse(HttpStatusCode.OK).ToString();
               jsonData.Success = true;
            }
            catch (DbUpdateConcurrencyException ex)
            {
                 jsonData.Error= Request.CreateErrorResponse(HttpStatusCode.NotFound, ex).RequestMessage.ToString();
            }

            return jsonData;
        }
         
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}